package com.juyu.miliao.api.service;

import com.juyu.miliao.api.config.IdCreater;
import com.juyu.miliao.api.dao.mysql.UserCountDao;
import com.juyu.miliao.api.dao.mysql.UserVideoDao;
import com.juyu.miliao.api.domain.UserVideo;
import com.juyu.miliao.api.util.PageUtil;
import com.juyu.miliao.common.constant.PageConstant;
import com.juyu.miliao.common.domain.BaseQuery;
import com.juyu.miliao.common.domain.PageIndex;
import com.juyu.miliao.common.exception.IllegalParamException;
import com.juyu.miliao.common.exception.InternalServerErrorException;
import com.juyu.miliao.common.exception.ResourceNotFoundException;
import com.juyu.miliao.common.util.CommonUtil;
import com.juyu.miliao.common.util.PageInfoUtil;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户视频管理
 *
 * @author jh
 */
@Service
public class UserVideoService {

    @Resource
    private UserVideoDao userVideoDao;

    @Resource
    private IdCreater idCreater;
    
    @Resource
    private UserCountDao userCountDao;

    /**
     * 获取用户视频列表,审核通过的
     *
     * @param viewUserId 浏览的用户id
     * @return List<UserVideo>
     */
    public List<UserVideo> list(Long viewUserId,int page,int pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        return userVideoDao.listUserVideoPass(viewUserId, startNum, pageSize);
    }

    /**
     * 用户视频列表
     *
     * @param currentUserId 当前用户id
     * @param viewUserId    浏览的用户id
     * @return List<UserVideo>
     */
    public List<UserVideo> listDetail(Long currentUserId, Long viewUserId,int page,int pageSize) {
        int startNum = PageUtil.getOffset(page, pageSize);
        List<UserVideo> list = new ArrayList<>();
        if (currentUserId == null || viewUserId == null) {
            throw new IllegalParamException("参数错误,用户id为空");
        }

        if (viewUserId.equals(currentUserId)) {
            //登录用户与浏览用户相同,存在审核中的视频
            list = userVideoDao.listUserVideoAll(viewUserId, startNum, pageSize);
        } else {
            //别人浏览
            list = userVideoDao.listUserVideoPass(viewUserId, startNum, pageSize);
        }
        return list;
    }

    /**
     * 保存用户视频
     *
     * @param userId    userId
     * @param userVideo userVideo
     */
    public void save(Long userId, UserVideo userVideo) {
        Long videoId = idCreater.newIdByUserId(userId);
        userVideo.setVideoId(videoId);
        userVideo.setUserId(userId);
        userVideo.setDescribe(CommonUtil.null2String(userVideo.getDescribe()));
        int result = userVideoDao.saveUserVideo(videoId, userVideo);
        if (result == 0) {
            throw new InternalServerErrorException("保存视频失败");
        }
    }


    /**
     * 删除用户视频
     *
     * @param userId  userId
     * @param videoId videoId
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void delete(Long userId, Long videoId) {
        UserVideo userVideo = userVideoDao.selectByVideoId(videoId);
        int result = userVideoDao.deleteUserVideo(userId, videoId);
        if (result == 0) {
            throw new ResourceNotFoundException("视频不存在或已删除");
        }else {
            if(userVideo != null){
                if(userVideo.getState() !=1){
                    userCountDao.reduceVideoNum(userId);
                }
            }
        }
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public JSONObject clickVideoLove(long userId, long videoId) {
        JSONObject json = new JSONObject();
        int result = userVideoDao.findClickVideoLove(userId, videoId);
        if (result > 0){
            userVideoDao.deleteUserVideoLove(userId, videoId);
            userVideoDao.deleteLoveNum(videoId);
            json.put("missage", "remove");
        } else{
            userVideoDao.addUserVideoLove(userId, videoId);
            userVideoDao.addLoveNum(videoId);
            json.put("missage", "add");
        }
        return json;
    }

}
